const app = getApp(); //引入导航

var util = require('../../utils/common.js'); //引入公共JS


var wxParse = require('../../wxParse/wxParse.js'); //引入


import { YcMakeup } from 'ycMakeup_model.js';
var ycMakeup = new YcMakeup();
var performId = '';
Page({
  data: {
    showIcon: true,
    //显示返回键以及首页
    stockFlag: true,
    //缺货登记
    show: false,
    //控制下拉列表的显示隐藏，false隐藏、true显示
    selectData: ['1', '2', '3', '4', '5', '6', '7', '8', '9'],
    //下拉列表的数据
    index: 0,
    //选择的下拉列表下标
    phone: '',
    //手机号
    number: '',
    //数量
    showMessage: false,
    //点击弹出的缺货登记提示显示
    messageContent: '',
    //点击弹出的缺货登记提示内容
    submitMessage: false,
    //点击弹出的缺货登记提示显示
    time: "",
    //默认选中第一个时间
    price: "",
    //默认选中第一个不是缺货的时间
    time_num: "",
    //默认选中第一个时间下标
    price_num: "",
    //默认选中第一个不是缺货的时间下标
    select_total: 0,
    //总票价
    num: 1,
    //默认选择的票价数量
    minusStatus: 'disabled',
    //数量小于1禁止点击状态
    noLogin: false,
    //未登录
    lineLogin: true //已登录

  },
  onLoad: function (options) {
    var aid = options.aid;
    tt.setStorageSync('aid', aid);
    var title = options.title;
    this.data.aid = aid;
    this.setData({
      aid: this.data.aid,
      title: title
    });

    this._loadDate(); //页面打开判断是否登陆过


    util.isLogin(this);
  },
  _loadDate: function () {
    //特殊说明
    // ycMakeup.getTebie(this.data.aid, (data) => {
    //   console.log(data)
    //  wxParse.wxParse('article', 'html', data.tebieshuoming, this, 5);
    //   this.setData({
    //     'shuoming': data.tebieshuoming
    //   });
    // });
    var that = this;
    ycMakeup.getBuKuanSJ(this.data.aid, rowdata => {
      tt.setStorageSync('performId', rowdata[0].id);
      var firstTime = rowdata[0].datetime;
      var firstID = rowdata[0].id;
      that.setData({
        listFare: rowdata,
        time_num: firstID,
        time: firstTime
      });
      console.log(rowdata[0].aid);
      console.log(rowdata[0].id);
      ycMakeup.getBuKuanPJ(rowdata[0].aid, rowdata[0].id, data => {
        console.log(data);
        that.setData({
          'listOrder': data
        });
      });
    });
  },
  queHuo: function (e) {
    var data = e.detail.value;
    var name = e.currentTarget.dataset.name;
  },
  selectTap: function (e) {
    // 点击下拉显示框
    this.setData({
      show: !this.data.show
    });
  },
  optionTap: function (e) {
    // 点击下拉列表
    var Index = e.currentTarget.dataset.index; //获取点击的下拉列表的下标

    this.setData({
      index: Index,
      show: !this.data.show
    });
  },
  phoneInput: function (e) {
    //监听手机号输入
    this.data.phone = e.detail.value;
  },
  wordInput: function (e) {
    //监听留言输入
    this.data.word = e.detail.value;
  },
  stockRefer: function () {
    var _index = this.data.index; //点击数量的下标

    var _phone = this.data.phone; //手机号

    var _num = this.data.selectData[_index]; //下拉选择的数量

    var _word = this.data.word; //留言

    var telRule = /^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[1|3|5|8|9])[0-9]{8}$/; //验证手机号

    if (_phone == '') {
      this.showMessage('手机号不能为空');
    } else if (!telRule.test(_phone)) {
      this.showMessage('手机号格式不正确');
    }
  },
  showMessage: function (text) {
    //打开关闭提示弹框
    var that = this;
    that.setData({
      showMessage: true,
      messageContent: text
    });
    setTimeout(function () {
      that.setData({
        showMessage: false,
        messageContent: ''
      });
    }, 3000);
  },
  selectTime: function (e) {
    //点击选择时间
    var _time = e.currentTarget.dataset.time; //点击选择获取时间

    var _num = e.currentTarget.dataset.num; //点击选择获取时间的下标

    var _id = e.target.id;

    if (_id == "disabled") {
      // 判断当缺货时打开缺货登记
      this.setData({
        stockFlag: false
      });
    } else {
      // 当点击非缺货时
      this.setData({
        time_num: _num,
        time: _time,
        'select_total': 0,
        price_num: "",
        num: 1,
        price: 0
      });
    }

    var aid = e.target.dataset.aid;
    performId = e.target.dataset.num;
    tt.setStorageSync('aid', aid);
    tt.setStorageSync('performId', performId); //时间id

    this.data.aid = aid;
    this.data.performId = performId;
    ycMakeup.getBuKuanPJ(this.data.aid, this.data.performId, data => {
      this.setData({
        'listOrder': data
      });
    });
  },
  selectPrice: function (e) {
    //选择时间
    var _price = e.currentTarget.dataset.price; //点击选择获取价格      真实价格

    var pmprice = e.currentTarget.dataset.pmprice; //点击选择获取价格  票面价格

    var _num = e.currentTarget.dataset.num; //点击选择获取价格的下标

    var pricetitle = e.currentTarget.dataset.pricetitle;
    var totalPrice = this.data.num * _price;
    var _id = e.target.id; //console.log(pricetitle)

    tt.setStorageSync('pmprice', pmprice);
    tt.setStorageSync('price', _price);
    tt.setStorageSync('quyuID', _num);
    tt.setStorageSync('priceID', _num);
    tt.setStorageSync('pricetitle', pricetitle);

    if (_id == "disabled") {
      // 判断当缺货时打开缺货登记
      this.setData({
        stockFlag: false
      });
    } else {
      // 当点击非缺货时
      this.setData({
        price_num: _num,
        price: _price,
        select_total: totalPrice
      });
    }
  },
  stockHide: function (e) {
    // 关闭缺货登记
    this.setData({
      stockFlag: true
    });
  },

  /* 点击减号 */
  bindMinus: function () {
    var num = this.data.num; //选择的数量

    var price = this.data.price; //选中的票价
    // 如果大于1时，才可以减

    if (num > 1) {
      num--;
    } // 只有大于一件的时候，才能normal状态，否则disable状态


    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    var totalPrice = num * price; // 将数值与状态写回

    this.setData({
      num: num,
      minusStatus: minusStatus,
      select_total: totalPrice
    });
  },

  /* 点击加号 */
  bindPlus: function () {
    var num = this.data.num;
    var price = this.data.price; //选中的票价
    // 数量最多可以选择10件

    if (num < 10) {
      num++;
    } // 只有大于一件的时候，才能normal状态，否则disable状态


    var minusStatus = num < 1 ? 'disabled' : 'normal';
    var totalPrice = num * price; // 将数值与状态写回

    this.setData({
      num: num,
      minusStatus: minusStatus,
      select_total: totalPrice
    });
  },
  submitClick: function (e) {
    var that = this;
    var num = this.data.num; //选择的数量

    var price = this.data.price; //选中的票价

    var time = this.data.time; //选中的时间

    var orderStatus = 0;
    var aid = tt.getStorageSync("aid");
    var performId = tt.getStorageSync("performId");
    var quyuID = tt.getStorageSync("quyuID");

    if (price == '' || time == '') {
      that.setData({
        submitMessage: true
      });
      setTimeout(function () {
        that.setData({
          submitMessage: false
        });
      }, 3000);
    } else {
      tt.navigateTo({
        url: '../ycTicket/ycTicket?aid=' + aid + '&num=' + num + '&performId=' + performId + '&quyuID=' + quyuID + '&orderStatus=' + orderStatus
      });
    }
  },
  //分享
  onShareAppMessage: function () {
    util.sharePage();
  },
  //小程序未登录时点击授权登录
  bindGetUserInfo: function (e) {
    var nickName = e.detail.userInfo.nickName;
    var avatarUrl = e.detail.userInfo.avatarUrl;
    var Name = {
      'nickName': nickName,
      'avatarUrl': avatarUrl
    };
    ycMakeup.getNickName(Name, res => {});
    util.noLogin(this);
  }
});